<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>主页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="Public/Plug/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="Public/Static/Home/css/main.css" media="all"/>
    <style>
        .layui-upload-file {
            display: none;
        }
        .margin-1{
            margin-top: 20px;
        }
        .dl-title-mar{
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .marleft-2{
            margin-left: 30px;
        }
        .json-pz-title{
            font-size: 18px;
            color: #333333;
        }
        .jsonfile-pz{
            color: red;
            margin-bottom: 10px;
            margin-left: 30px;
            margin-top: 10px;
        }
        .delInp{
            width: 7px;
            height: 22px;
            padding: 0px 5px;
            margin-left: 10px;
            background-color: #009688;
            color: #fff;
            white-space: nowrap;
            text-align: center;
            border-radius: 2px;
            cursor: pointer;
            opacity: .9;
            display: inline-block;
        }
        .delbtn{
            width: 7px;
            height: 22px;
            padding: 0px 5px;
            background-color: #009688;
            color: #fff;
            white-space: nowrap;
            text-align: center;
            border-radius: 2px;
            cursor: pointer;
            opacity: .9;
            display: inline-block;
        }
        .tmoba-content{
            margin-bottom: 10px;
            position: relative;
        }
    </style>
</head>
<body class="childrenBody">
<div style="padding: 20px 10px;width: 100%;height: 100px;">

    当前资料库对应版本{$versionNumber}, 可切换到
    <div style="display: inline-block;" class="layui-form-item">
        <select id="testSelectVersion" name="versionNum" lay-verify="required">
            {foreach $resVersionTotal as $val}
            <option value="{$val.id}" {if $val.versionNumber==$versionNumber}selected {/if}>{$val.versionNumber}</option>
            {/foreach}
        </select>
    </div>
        当前资料库对应季度{$seasonNumber}, 可切换到
        <div style="display: inline-block;" class="layui-form-item">
            <select id="testSelectSeason" name="versionNum" lay-verify="required">
                {foreach $resSeasonTotal as $val}
                <option value="{$val.id}" {if $val.seasonNumber==$seasonNumber}selected {/if}>{$val.seasonNumber}</option>
                {/foreach}
            </select>
        </div>
    <button style="margin-left: 20px;" class="layui-btn rollback">切换</button>


    <button style="margin-left: 10px;" class="layui-btn copy">复制数据</button>
    <button style="margin-left: 5%;" class="layui-btn layui-btn-danger makeAllJs">生成全部js</button>



</div>

<fieldset style="width: 80%;" class="layui-elem-field">
    <legend>JSON版本数据</legend>

    <div class="layui-field-box">

        <div style="width: 100%;height: 60px;padding: 0px 0px 10px 30px;">
            <form id="import-form" enctype="multipart/form-data" style="display: inline-block">
            <select  name="dataType" lay-verify="required" style=" height: 36px">
                <option value="">选择数据</option>
                <option value="1">种族</option>
                <option value="2">职业</option>
                <option value="3">英雄</option>
                <option value="4">棋子</option>
                <option value="5">道具</option>

            </select>
            <div style="display: inline-block;width: 47%;">
                <input  style="width: 600px;" type="file" name="json" placeholder="上传最新的Json文件"
                       autocomplete="off" class="layui-input">
            </div>

            </form>
            <button class="layui-btn update" >更新到数据库</button>
        </div>


    </div>


</fieldset>

<form id="delete-form" style="border: #999999;margin-left: 45px;display: inline-block">

    <select  name="dataType" lay-verify="required" style=" height: 36px">
        <option value="">选择数据</option>
        <option value="1">种族</option>
        <option value="2">职业</option>
        <option value="3">英雄</option>
        <option value="4">棋子</option>
        <option value="5">道具</option>
    </select>
    <input name="id" oninput="value=value.replace(/[^\d]/g,'')" style="display: inline-block;width: 150px" class="layui-input" placeholder="对应ID(不填全部删除)" type="text" >

</form>
<button class="layui-btn delete" >删除数据</button>
<script type="text/javascript" src="Public/Plug/layui/layui.all.js"></script>
<script type="text/javascript" src="Public/Static/Home/js/main.js"></script>
<script>
    layui.use(['element', 'upload','form'], function () {
        var $ = layui.jquery,
            element = layui.element,
            form = layui.form
        upload = layui.upload;



        //切换版本和季度
        $(".rollback").on("click",function () {
            layer.confirm('确定切换？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                //获取Id
                var versionId = $('#testSelectVersion option:selected') .val();//索引


                var seasonId = $('#testSelectSeason option:selected') .val();//索引


                //按钮【按钮一】的回调
                var url = "{U('Main/switchData')}";
                $.ajax({
                    url: url, type: "post", dataType: "json",
                    data: { versionId:versionId,seasonId:seasonId},
                    beforeSend:function () {
                        this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                    },
                    success:function(data){
                        if(data.status =="error"){
                            layer.msg(data.msg);//失败的表情
                            return;
                        }else if(data.status == "success"){
                            layer.msg(data.msg, {
                                icon: 1,//成功的表情
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                top.location.reload();
                            });
                        }
                    },
                    complete: function () {
                        layer.close(this.layerIndex);
                    },
                });

            });
        });


        //生成js
        $(".makeAllJs").on("click",function () {
            layer.confirm('确认生成当前版本已发布数据js文件?',{
                title: '提示信息',
                btn: ['确定', '取消']
            }, function(index, layero){
                //do something
                //按钮【按钮一】的回调
                var url = "{U('main/makeJs')}";
                $.ajax({
                    url: url, type: "post", dataType: "json",
                    beforeSend:function () {
                        this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                    },
                    success:function(data){
                        layer.closeAll('dialog');  //加入这个信息点击确定 会关闭这个消息框
                        if(data.status =="error"){
                            layer.msg(data.msg,{
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            });
                            return;
                        }else if(data.status =="success"){
                            layer.closeAll('dialog');  //加入这个信息点击确定 会关闭这个消息框
                            layer.msg(data.msg, {
                                icon: 1,//成功的表情
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                top.location.reload();
                            });
                        }
                    },
                    complete: function () {
                        layer.close(this.layerIndex);
                    },
                });
                layer.close(index);
            });
        });


        //导入数据
        function insertJson(){

            //数据导入
            var url = "{U('Main/insertJson')}";

            var data = new FormData()
            data.append('dataType',$("#import-form").find("[name='dataType']").val());
            data.append('jsonFile', $("#import-form").find("[name='json']")[0].files[0])


            $.ajax({
                type: 'POST',
                url: url,
                dataType: "json",
                data: data,
                cache: false,
                processData: false,
                contentType: false,
                success: function (data) {
                    if(data.status =="error"){
                        layer.msg(data.msg,{
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        return;
                    }else if(data.status =="success"){
                        layer.closeAll('dialog');  //加入这个信息点击确定 会关闭这个消息框
                        layer.msg(data.msg, {
                            icon: 1,//成功的表情
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            top.location.reload();
                        });
                    }
                }
            });
        }

        //更新到数据库
        $(".update").on("click",function () {



            if($("#import-form").find("[name='json']")[0].files[0]===undefined) {
                layer.msg('请选择要同步的json');
                return;
            }
            var dataType = $("#import-form").find("[name='dataType']").val()
            if(!dataType) {
                layer.msg('请选择要同步的数据类型');
                return;
            }
            var index = layer.confirm('确定同步到当前版本和季度？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                layer.close(index);
                insertJson();
            });
        });




        //复制版本和季度
        $(".copy").on("click",function () {
            layer.confirm('确定复制此当前版本和季度？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                layer.close(layer.index);

                layer.prompt({ title: '请输入要命名的版本号和季度号(“,”隔开)，并确认',formType: 0}, function(str, index){
                    var arr  = str.split(',');
                    var versionNumber = arr[0] ? arr[0] : '';
                    var seasonNumber = arr[1] ? arr[1] : '';

                    if(versionNumber == ""){
                        layer.msg("版本号不能为空", {
                            icon: 2,//失败的表情
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        return;
                    }
                    if(versionNumber.length>50){
                        layer.msg("版本号过长", {
                            icon: 2,//失败的表情
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        return;
                    }
                    if(seasonNumber == ""){
                        layer.msg("季度不能为空", {
                            icon: 2,//失败的表情
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        return;
                    }
                    {literal}
                    var regu =/^[0-9]{4}.S[0-9]{1}$/;
                    {/literal}
                    if(!regu.test(seasonNumber)){
                        layer.msg("季度格式错误", {
                            icon: 2,//失败的表情
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        });
                        return;
                    }
                    layer.close(index);
                    var url = "{U('Main/copy')}";
                    $.post(url,{ versionNumber:versionNumber,seasonNumber:seasonNumber},function(data){
                        if(data.status !="error"){
                            layer.msg(data.msg, {
                                icon: 1,//成功的表情
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                location.reload();
                            });
                            return;
                        }else{
                            layer.msg(data.msg, {
                                icon: 2,//失败的表情
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                              //  location.reload();
                            });
                            return;
                        }
                    });
                });
            });
        });

        //删除当天版本和季度的数据

        $(".delete").on('click',function () {
            var dataType = $("#delete-form").find("[name='dataType']").val();
            var dataTypeName = $("#delete-form").find("option:selected").text();

            if(!dataType) {
                layer.msg('请选择要删除的数据类型');
                return;
            }
            var id = $("#delete-form").find("[name='id']").val();

            var text = id ? '"ID为'+id+'"' :'"全部"';

            var index = layer.confirm('确定删除'+text+'的"'+dataTypeName+'"数据？', {
                btn: ['确认','取消'] //按钮
            }, function(){
                layer.close(index);
                var url = "{U('Main/deleteData')}";
                $.ajax({
                    url: url, type: "post", dataType: "json",
                    data: { dataType:dataType,id:id},
                    beforeSend:function () {
                        this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                    },
                    success:function(data){
                        if(data.status =="error"){
                            layer.msg(data.msg);//失败的表情
                            return;
                        }else if(data.status == "success"){
                            layer.msg(data.msg, {
                                icon: 1,//成功的表情
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                top.location.reload();
                            });
                        }
                    },
                    complete: function () {
                        layer.close(this.layerIndex);
                    },
                });
            });
        })

    });
</script>
</body>
</html>
